package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.*;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.pojos.Wmdownorup;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {
    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private WmUserService wmUserService;

    @PostMapping("/list")
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {
        return wmNewsService.submitNews(dto);
    }
    //下架
    @PostMapping("/down_or_up")
    public ResponseResult xj(@RequestBody Wmdownorup wmdownorup) {
        ResponseResult up = wmNewsService.up(wmdownorup);
        log.info("我在这里");
        return up;
    }
    @PostMapping("/list_vo")
    public ResponseResult listvo(@RequestBody WmadminNewsDto dto){
        log.info("没问题啦");
        Page<WmNews> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus()!=null){
            queryWrapper.eq(WmNews::getStatus,dto.getStatus());
        }
        if (dto.getTitle()!=null&&!"".equals(dto.getTitle())){
            queryWrapper.eq(WmNews::getTitle,dto.getTitle());
        }
        Page<WmNews> page1 = wmNewsService.page(page, queryWrapper);
        List<WmNews> records = page1.getRecords();
        ArrayList<WmadminDto> wmadminDtos = new ArrayList<>();
        for (WmNews record : records) {
            WmadminDto wmadminDto = new WmadminDto();
            BeanUtils.copyProperties(record,wmadminDto);
            Integer userId = record.getUserId();
            WmUser wmUser = wmUserService.getById(userId);
            wmadminDto.setAuthorName(wmUser.getName());
            wmadminDtos.add(wmadminDto);
        }
        return ResponseResult.okResult(wmadminDtos);
    }
    @GetMapping("/one_vo/{id}")
    public ResponseResult getId(@PathVariable Long id){
        WmadminDto wmadminDto = new WmadminDto();
        WmNews wmNews = wmNewsService.getById(id);
        BeanUtils.copyProperties(wmNews,wmadminDto);
        WmUser wmUser = wmUserService.getById(wmNews.getUserId());
        wmadminDto.setAuthorName(wmUser.getName());
        return ResponseResult.okResult(wmadminDto);
    }
    @PostMapping("/auth_pass")
    public ResponseResult ok(@RequestBody WmAuditDto dto){
        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getId());
        wmNews.setStatus(dto.getStatus());
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult("ok");
    }
    @PostMapping("/auth_fail")
    public ResponseResult er(@RequestBody WmAuditDto dto){

        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getId());
        wmNews.setStatus(dto.getStatus());
        wmNews.setReason(dto.getMsg());
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult("ok");
    }
}